package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.app.InfantApp;
import com.android.infantschool.bean.BeautifulTeacherBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class BeautifulTeacherAdapter extends RecyclerView.Adapter<BeautifulTeacherAdapter.MyHolder> {

    private Context mContext;
    public List<BeautifulTeacherBean.DataBean> list = null;
    private OnLister lister;

    public BeautifulTeacherAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_beautiful_teacher, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        BeautifulTeacherBean.DataBean bean = list.get(position);

        if (position == 0 || position == 1 || position == 2) {
            viewHolder.tvRanking.setVisibility(View.GONE);
            viewHolder.imgRanking.setVisibility(View.VISIBLE);
            if (position == 0) {
                Glide.with(mContext).load(R.mipmap.icon_number_one)
                        .into(viewHolder.imgRanking);
            }
            if (position == 1) {
                Glide.with(mContext).load(R.mipmap.icon_number_two)
                        .into(viewHolder.imgRanking);
            }
            if (position == 2) {
                Glide.with(mContext).load(R.mipmap.icon_number_three)
                        .into(viewHolder.imgRanking);
            }
        } else {
            viewHolder.tvRanking.setVisibility(View.VISIBLE);
            viewHolder.imgRanking.setVisibility(View.GONE);
            viewHolder.tvRanking.setText("" + position);
        }

        Glide.with(mContext).load(bean.getTeacherImg())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 100))
                .override(150, 150)
                .placeholder(R.mipmap.icon_user)
                .error(R.mipmap.icon_user)
                .into(viewHolder.imgTeacherIcon);

        viewHolder.tvTeacherName.setText(bean.getName());
        viewHolder.tvPerformance.setText(String.valueOf(bean.getAchievements()));
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<BeautifulTeacherBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<BeautifulTeacherBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgRanking;
        public TextView tvRanking;
        public ImageView imgTeacherIcon;
        public TextView tvTeacherName;
        public TextView tvPerformance;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgRanking = itemView.findViewById(R.id.img_ranking);
            tvRanking = itemView.findViewById(R.id.tv_ranking);
            imgTeacherIcon = itemView.findViewById(R.id.img_teacher_icon);
            tvTeacherName = itemView.findViewById(R.id.tv_teacher_name);
            tvPerformance = itemView.findViewById(R.id.tv_performance);
        }
    }

    public interface OnLister {
        void lister(int position, int type);
    }
}